package se.webbzon.boltzmann.game.object.dialogue.word;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Iterator;

import se.webbzon.boltzmann.game.object.dialogue.AbstractDialogue;
import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.GroupAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleGroupAppearance3D;

public abstract class DividedDialogueWord extends AbstractDialogueWord {
	
	// The word to print
	private final String word;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new divided dialogue word with the default color. **/
	public DividedDialogueWord(DialogueLayout layout, String word) {
		this(layout,word,DialogueLayout.defaultColor);
	}
	
	/** Creates a new divided dialogue word with a given color. **/
	public DividedDialogueWord(DialogueLayout layout, String word, Color color) {
		super(layout, createAppearance(layout,word,color));
		this.word = word;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the (n-1):th divided dialogue character associated with
	 * this divided dialogue word. **/
	public DividedDialogueCharacter getCharacter(int index) {
		SimpleGroupAppearance3D appearance = 
				(SimpleGroupAppearance3D) getAppearance();
		return (DividedDialogueCharacter) appearance.getAppearance(index);
	}
	
	@Override public void getDimension(Dimension d) {
		final DialogueLayout layout = getLayout();
		d.setSize(	getWord().length() * layout.getCharacterWidth(),
					layout.getCharacterHeight());
	}

	@Override public String getWord() {
		return word;
	}
	
	@Override public void onUpdateWord(AbstractDialogue dialogue) {};
	
	@Override public void onShowCharacter(AbstractDialogue dialogue, int character) {
		SimpleGroupAppearance3D appearance = 
				(SimpleGroupAppearance3D) getAppearance();
		Iterator<Appearance3D> it = appearance.getAppearances().iterator();
		int index = 0;
		while (it.hasNext()) {
			if (index < character)
				it.next().setVisible(true);
			else
				it.next().setVisible(false);
			index++;
		}
	}
	
	@Override public void onShowAllCharacters(AbstractDialogue dialogue) {
		onShowCharacter(dialogue,getWord().length());
	}

	@Override public boolean isNewLine() {
		return false;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates a collection of text appearances where each character
	 * in the given word is a member of the collection. **/
	private static Appearance3D createAppearance(DialogueLayout layout,
															String word, 
															Color color) {
		// Create character list
		ArrayList<Appearance3D> characters = new ArrayList<Appearance3D>();
		
		for (int i = 0; i < word.length(); i++) {
			final DividedDialogueCharacter character = 
					new DividedDialogueCharacter(layout,word.charAt(i),color);
			characters.add(character);
			character.setVisible(false);
		}
		
		// Return the group appearance
		return new SimpleGroupAppearance3D(	1,
											GroupAppearance3D.TRANSPARENCY,
											characters);
	}

}
